package cn.geckos.fml
{
	import flexunit.framework.TestCase;
	import flexunit.framework.TestSuite;
	import cn.geckos.fml.data.TextData;
	import cn.geckos.fml.data.LinkData;
	import cn.geckos.fml.data.StackData;
	import cn.geckos.fml.data.PictureData;

	public class FMLParserTest extends TestCase
	{
		public function FMLParserTest(methodName:String):void
		{
			super(methodName);
		}
		
		public static function testsute():TestSuite
		{
			var ts:TestSuite = new TestSuite();
			ts.addTest(new FMLParserTest("testStack"));
			return ts;
		}
		
		/* test parse data */
		
		public function testTextData():void
		{
			var xml:XML = 
						  <FML>
						  	<text id='name'>
						  		hello my name is jeff
						  	</text>
						  	<text id='sex' value='男'/>
						  </FML>;
			
			var t1:TextData = new TextData();
			t1.id = "name";
			t1.text = "hello my name is jeff";
			
			var t2:TextData = new TextData();
			t2.id = "sex";
			t2.text = "男";
			
			var ft:FMLParser = new FMLParser(xml);
			var a:Array = ft.deserialize();
			
			var rt1:TextData = a[0] as TextData;
			var rt2:TextData = a[1] as TextData;
			assertTrue(rt1.equals(t1));
			assertTrue(rt2.equals(t2));
		}
		
		public function testLink():void
		{
			var xml:XML = 
						  <FML>
						  	<link id='home' name='home' host='http://127.0.0.1/home'>
						  		<param name='jid' value='123'/>
						  		<param name='name' value='jeff'/>
						  	</link>
						  	<link id='forum' name='forum' host='http://geckos.cn/forum'/>
						  </FML>;
			
			var link1:LinkData = new LinkData();
			link1.id = "home";
			link1.host = "http://127.0.0.1/home";
			link1.name = "home";
			link1.setParam("jid", "123");
			link1.setParam("name", "jeff");
			
			var link2:LinkData = new LinkData();
			link2.id = "forum";
			link2.host = "http://geckos.cn/forum";
			link2.name = "forum";
			
			var ft:FMLParser = new FMLParser(xml);
			var a:Array = ft.deserialize();
			
			var rt1:LinkData = a[0] as LinkData;
			var rt2:LinkData = a[1] as LinkData;
			
			assertTrue(rt1.equals(link1));
			assertTrue(rt2.equals(link2));
		}
		
		public function testStack():void
		{
			var xml:XML = 
						  <FML>
						  	<stack id='content'>
						  		<text id='name'>
						  			hello my name is jeff
						  		</text>
						  		<link id='home' name='home' host='http://127.0.0.1/home'>
						  			<param name='jid' value='123'/>
						  			<param name='name' value='jeff'/>
						  		</link>
						  		<text id='sex' value='男'/>
						  		<link id='forum' name='forum' host='http://geckos.cn/forum'/>
						  	</stack>
						  </FML>;
			var st:StackData = new StackData();
			st.id = "content";
			
			var t1:TextData = new TextData();
			t1.id = "name";
			t1.text = "hello my name is jeff";
			st.push(t1);
			
			var l1:LinkData = new LinkData();
			l1.id = "home";
			l1.host = "http://127.0.0.1/home";
			l1.name = "home";
			l1.setParam("jid", "123");
			l1.setParam("name", "jeff");
			st.push(l1);
			
			var t2:TextData = new TextData();
			t2.id = "sex";
			t2.text = "男";
			st.push(t2);
			
			var l2:LinkData = new LinkData();
			l2.id = "forum";
			l2.host = "http://geckos.cn/forum";
			l2.name = "forum";
			st.push(l2);
			
			var ft:FMLParser = new FMLParser(xml);
			var a:Array = ft.deserialize();
			
			var stk:StackData = a[0] as StackData;
			
			assertTrue(stk.equals(st));
		}
		
		/* test equals function in each type of IData class*/
		
		public function testTextEquals():void
		{
			var t1:TextData = new TextData();
			t1.id = "hello";
			t1.text = "how are you?你好吗？";
			var t2:TextData = new TextData();
			t2.id = "hello";
			t2.text = "how are you?你好吗？";
			var t3:TextData = new TextData();
			t3.id = "hello";
			t3.text = "jeff";
			
			assertTrue(t1.equals(t2));
			assertFalse(t1.equals(t3));
		}
		
		public function testPictureEquals():void
		{
			var p1:PictureData = new PictureData();
			p1.id = "my face";
			p1.link = "http://127.0.0.1/my_face/jeff.jpg";
			p1.name = "jeff_face";
			var p2:PictureData = new PictureData();
			p2.id = "my face";
			p2.link = "http://127.0.0.1/my_face/jeff.jpg";
			p2.name = "jeff_face";
			var p3:PictureData = new PictureData();
			p3.id = "my face";
			p3.link = "http://127.0.0.1/my_face/jeff.jpg";
			p3.name = "harry_face";
			
			assertTrue(p1.equals(p2));
			assertFalse(p1.equals(p3));
		}
		
		public function testLinkEquals():void
		{
			var l1:LinkData = new LinkData();
			l1.id = "home";
			l1.host = "http://geckos.cn/forum/";
			l1.name = "forum home";
			var l2:LinkData = new LinkData();
			l2.id = "home";
			l2.host = "http://geckos.cn/forum/";
			l2.name = "forum home";
			var l3:LinkData = new LinkData();
			l3.id = "home";
			l3.host = "http://geckos.cn/forum/";
			l3.name = "forum home";
			l3.setParam("jid", "123456");
			l3.setParam("thread", "my_home");
			var l5:LinkData = new LinkData();
			l5.id = "home";
			l5.host = "http://geckos.cn/forum/";
			l5.name = "forum home";
			l5.setParam("jid", "123456");
			l5.setParam("thread", "my_home");
			var l4:LinkData = new LinkData();
			l4.id = "home";
			l4.host = "http://geckos.cn/forum/";
			l4.name = "forum home";
			l4.setParam("jid", "123456");
			l4.setParam("thread", "my_home");
			l4.setParam("name", "jeff");
			
			assertTrue(l1.equals(l2));
			assertTrue(l3.equals(l5));
			assertFalse(l1.equals(l3));
			assertFalse(l3.equals(l4));
		}
		
		public function testStackEquals():void
		{
			var st1:StackData = new StackData();
			
			var l1:LinkData = new LinkData();
			l1.id = "home";
			l1.host = "http://geckos.cn/forum/";
			l1.name = "forum home";
			l1.setParam("jid", "123456");
			l1.setParam("thread", "my_home");
			l1.setParam("name", "jeff");
			st1.push(l1);
			
			var p1:PictureData = new PictureData();
			p1.id = "my face";
			p1.link = "http://127.0.0.1/my_face/jeff.jpg";
			p1.name = "jeff_face";
			st1.push(p1);
			
			var t1:TextData = new TextData();
			t1.id = "hello";
			t1.text = "how are you?你好吗？";
			st1.push(t1);
			
			var st2:StackData = new StackData();
			var l2:LinkData = new LinkData();
			l2.id = "home";
			l2.host = "http://geckos.cn/forum/";
			l2.name = "forum home";
			l2.setParam("jid", "123456");
			l2.setParam("thread", "my_home");
			l2.setParam("name", "jeff");
			st2.push(l2);
			
			var p2:PictureData = new PictureData();
			p2.id = "my face";
			p2.link = "http://127.0.0.1/my_face/jeff.jpg";
			p2.name = "jeff_face";
			st2.push(p2);
			
			var t2:TextData = new TextData();
			t2.id = "hello";
			t2.text = "how are you?你好吗？";
			st2.push(t2);
			
			var st3:StackData = new StackData();
			var l3:LinkData = new LinkData();
			l3.id = "home";
			l3.host = "http://geckos.cn/forum/";
			l3.name = "forum home";
			l3.setParam("jid", "123456");
			l3.setParam("thread", "my_home");
			l3.setParam("name", "jeff");
			l3.setParam("hi", "google");
			st3.push(l3);
			
			var p3:PictureData = new PictureData();
			p3.id = "my face";
			p3.link = "http://127.0.0.1/my_face/jeff.jpg";
			p3.name = "jeff_face";
			st3.push(p3);
			
			var t3:TextData = new TextData();
			t3.id = "hello";
			t3.text = "how are you?你好吗？";
			st3.push(t3);
			
			assertTrue(st1.equals(st2));
			assertFalse(st1.equals(st3));
		}
	}
}